﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

namespace MqttShutdown
{
    class Program
    {
        static void Main(string[] args)
        {
            MqttClient client;

            string brokerHostName = "gzhwedu.cn";
            int brokerPort = 1883;
            string clientId = "client2934";

            string[] topic = { "action/+/home/202/wol" };
            byte[] qosLevels = { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE };

            client = new MqttClient(brokerHostName, brokerPort, false, null, null, MqttSslProtocols.None);

            client.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(messageReceive);

            //client.ConnectionClosed += (o, e) => { 
            //    try {
            //        Console.WriteLine("连接中断，正在重连!");
            //        client.Connect(clientId);
            //        Console.WriteLine("连接成功!");
            //        client.Subscribe(topic, qosLevels);
            //    } 
            //    catch  
            //    { } 
            //};
            //连接Broker
            //client.Connect(clientId);
            //Console.WriteLine("连接成功!");
            ////client.Connect(clientId, username, password);

            //client.Subscribe(topic, qosLevels);

            while(true)
            {
                if (!client.IsConnected)
                {
                    try
                    {
                        Console.WriteLine("正在连接......");
                        client.Connect(clientId);
                        Console.WriteLine("连接成功!");
                        client.Subscribe(topic, qosLevels);
                    }
                    catch(Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                Thread.Sleep(10000);

            }
            Console.ReadLine();
        }


        private static void messageReceive(object sender, MqttMsgPublishEventArgs e)
        {
            string mac_addr = Mac2String(e.Message);
            Console.WriteLine(mac_addr);
            List<string> macs = GetMacByNetworkInterface();
            if(macs.Contains(mac_addr))
            {
                Console.WriteLine("即将开始休眠");
                Application.SetSuspendState(PowerState.Hibernate, true, true);
            }

        }

        static string Mac2String(byte[] mac)
        {
            return string.Join("", (from b in mac select b.ToString("X2")).ToArray());
        }
        /// <summary>
        /// 获取所有网卡物理地址
        /// </summary>
        /// <returns></returns>
        public static List<string> GetMacByNetworkInterface()
        {
            List<string> macs = new List<string>();
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces)
            {
                macs.Add(ni.GetPhysicalAddress().ToString());
            }
            return macs;
        }
    }
}
